//
//  MoreCourseViewController.m
//  TeacherNet
//
//  Created by slz on 2017/11/9.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "MoreCourseViewController.h"
#import "CourseTypeListModel.h"
#import "MoreCourseTableViewCell.h"

@interface MoreCourseViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) YWTableView *tableView;
@property (nonatomic,strong) NSMutableArray *courseTypeArray;
@end

@implementation MoreCourseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _courseTypeArray = @[].mutableCopy;
    self.showNavigationBar = YES;
    [self setControllerTitle:@"全部课程"];
    UIBarButtonItem * backItem = [UIBarButtonItem itemWithImage:@"back_icon" selectImage:@"" addTarget:self andAction:@selector(backAction)];
    self.navigationItem.leftBarButtonItem = backItem;
    
    _tableView = [[YWTableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    
    [self requestData];
    
    // Do any additional setup after loading the view.
}

- (void)requestData{
    [BaseHttpTool get:kAPI_MORE_COURSE params:nil success:^(id responseObj) {
        [self dataToModelArray:responseObj];
    } failure:^(NSError *error) {
        [self.view showTip:@" request error"];
    }];
}

-(void) backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)dataToModelArray:(id)responseObj{
    [_courseTypeArray removeAllObjects];
    MFWJson * json = [MFWJson jsonWithObject:responseObj];
    NSArray * arr = [[json jsonForKey:@"classifyList"] toArray];
    if (!arr || arr.count ==0) return;
    for (NSDictionary * courseTypeDict in arr ) {
        CourseTypeListModel * model = [CourseTypeListModel yy_modelWithDictionary:courseTypeDict];
        [_courseTypeArray addObject:model];
    }
    [_tableView reloadData];
}

#pragma  mark TableViewDelegate && TableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * reuseIdentifier = @"MoreCourseTableViewCell";
    MoreCourseTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell){
        cell = [[MoreCourseTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    CourseTypeListModel * model = _courseTypeArray[indexPath.section];
    NSArray * contentArray = model.courseListModel;
    CourseTypeListModel *lModel = indexPath.row*2 < [contentArray count] ? [contentArray objectAtIndex:indexPath.row*2] : nil;
    CourseTypeListModel *rModel = indexPath.row*2+1 < [contentArray count] ? [contentArray objectAtIndex:indexPath.row*2+1] : nil;
    [cell setWithLeftModel:lModel rightModel:rModel];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    CourseTypeListModel * model = _courseTypeArray[section];
    NSInteger courseCount = model.courseListModel.count;
    return courseCount % 2 == 0 ? courseCount/2 : courseCount/2 + 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _courseTypeArray.count;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 37)];
    
    CALayer * lineLayer = [CALayer new];
    lineLayer.backgroundColor = kLineColor_Gray_Default.CGColor;
    lineLayer.frame = CGRectMake(0, 0, kScreenWidth, 7);
    [backView.layer addSublayer:lineLayer];
    
    UIImageView * imageView = [UIImageView new];
    imageView.image = [UIImage imageNamed:@"course_type_arrow"];
    imageView.size = CGSizeMake(22, 22);
    imageView.centerY = 25.5;
    imageView.left = 18;
    [backView addSubview:imageView];
    
    UILabel * titleLabel = [UILabel new];
    titleLabel.size = CGSizeMake(150, 37);
    titleLabel.left = imageView.right + 5;
    titleLabel.top = 7;
    titleLabel.textColor = kDark_TextColor_Default;
    titleLabel.fontSize = 15;
    [backView addSubview:titleLabel];
    
    CourseTypeListModel * model = _courseTypeArray[section];
    titleLabel.text = model.courseTypeName;
    
    return backView;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
